package photostock.control.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import photostock.model.Album;
import photostock.model.User;
import photostock.model.UserItem;

/**
 * Servlet implementation class AlbumDetail
 */
public class AlbumDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		System.out.println("action : " + request.getParameter("action"));
		if (null == request.getParameter("action")) {
			int id = 0;
			try {
				id = Integer.parseInt(request.getParameter("albumId"));
			} catch (Exception e) {
				System.out.println(e.getClass().getName() + " : " + e.getMessage());
			}
			Album current = null;
			for (UserItem o : ((User)session.getAttribute("user")).getChildrenItems()) {
				Album al = (Album)o;
				if (al.getId() == id) {
					current = al;
					System.out.println(current.getWebPath());
					System.out.println(current.getHardPath());
					break;
				}
			}
			session.setAttribute("currentAlbum", current);
			getServletContext().getRequestDispatcher("/jsp/albumDetail.jsp").forward(request,response);
			return;
		} else if (!request.getParameter("action").equals("result")) {
			getServletContext().getRequestDispatcher("/jsp/photo" + request.getParameter("action") + ".jsp").forward(request,response);
			return;
		} else {
			if (ServletFileUpload.isMultipartContent(request)) {	
			ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory() ); 
			try { 
			 	List<?> list = upload.parseRequest(request); 
			 	Iterator<?> it = list.iterator();
			 	while (it.hasNext()) { 
				 	FileItem item = (FileItem)it.next(); 
				 	if (!item.isFormField()) { 
				   		File fullFile=new File(item.getName()); 
				   		File savedFile=new File(getServletContext().getRealPath("/tmp") + System.getProperty("file.separator")+ fullFile.getName()); 
				   		savedFile.deleteOnExit();
				   		System.out.println(savedFile.getName());
				   		item.write(savedFile); 
				   		
				   		
				   		
				   		File dest = new File(((Album)session.getAttribute("currentAlbum")).getHardPath()+ UserItem.FS + savedFile.getName());
				   		savedFile.renameTo(dest);
						System.out.println(savedFile.exists() + "***" + dest.exists() + "***" + dest.getName()); 
						//ItemFactory.createPhoto(((Album)session.getAttribute("currentAlbum")), ((Album)session.getAttribute("currentAlbum")).getItems().size(), dest.getName());
						getServletContext().getRequestDispatcher("/jsp/albumDetail.jsp").forward(request,response);
						return;
				 	}
			 	}
			} catch (Exception e) {
				System.out.println(e.getClass().getName() + " : " + e.getMessage());
			}
			}
		}
	}

}
